<template>
  <div>
    <el-table :data="tableData" style="width: 100%" border v-loading="loading">
      <el-table-column prop="id" label="ID" min-width="50px" type="selection"></el-table-column>
      <el-table-column prop="username" min-width="40px" label="姓名"></el-table-column>
      <el-table-column prop="phone" label="电话" min-width="50px"></el-table-column>
      <el-table-column prop="email" label="邮箱"></el-table-column>
      <el-table-column prop="age" label="年龄" min-width="25px"></el-table-column>
      <el-table-column prop="dept" label="所属部门" min-width="60px"></el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column prop="birthday" label="生日" min-width="50px"></el-table-column>
      <el-table-column align="center" sortable prop="state" label="状态" min-width="50">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.state=='1'?show:nshow" active-color="#13ce66" inactive-color="#ff4949"
                     @change="editType(scope.$index, scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="address" label="地址"></el-table-column>
      <el-table-column prop="" label="操作">
        <template slot-scope="scope">
          <el-button size="mini" type="primary" icon="el-icon-edit" @click="handleEdit(scope.$index, scope.row)">编辑
          </el-button>
          <el-button size="mini" type="danger" icon="el-icon-delete" @click="deleteDept(scope.$index, scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--
     @size-change：选择了每页条数 ，触发handleSizeChange方法
     @current-change：选择了第几页 ，触发handleCurrentChange方法
     :currentPage：把当前页绑定到  currentPage 变量
     :page-sizes="pageSizes" ：可选的每页条数
     :page-size="pageSize" ：每页条数
     :layout  ：分页条布局
     :total="tableTotal" ：总条数
      -->
    <el-pagination
        style="margin-top: 20px"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="pageSizes"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>
  </div>
</template>
<script>

export default {
  data() {
    return {
      page: 1,  //第几页
      size: 10, //每页条数
      total: 0,
      pageSizes: [5, 10, 20, 50, 100],
      tableData: [],
      loading: true,
      nshow: false,
      show: true
    }
  },

  mounted() {
    this.getList();
  },

  methods: {
    editType(index, row) {
      if (row.state == 1) {
        row.state = 0;
      } else {
        row.state = 1
      }
    },

    getList() {
      let param = {"currentPage": this.page, "pageSize": this.size}
      this.$http.post('/emp/list', param).then((res) => {
        this.tableData = res.data.data;
        this.total = res.data.totals
      });
      this.loading = false
    },

    handleCurrentChange(val) {
      this.page = val
      this.getList();
    },

    handleSizeChange(val) {
      this.size = val
      this.getList();
    }
  }

}
</script>

<style>
el-table-column {
  alignment: center;
}
</style>
